<template>
  <div class="account_form">
    <el-form ref="formRef"
    :model="form"
    label-width="90px"
    :rules="rules">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="form.name"></el-input>
     </el-form-item>
     <el-form-item label="密码" prop="password">
        <el-input v-model="form.password"  show-password></el-input>
     </el-form-item>
    </el-form>
  </div>
</template>
<script lang="ts">
import { reactive, defineComponent, ref } from 'vue'
import { ElForm } from 'element-plus'
import { useStore } from 'vuex'
import Localcache from '@/utils/cache'
export default defineComponent({
  setup() {
    const store = useStore()
    const form = reactive({
      name: Localcache.getCache('name')?? '',
      password: Localcache.getCache('password')?? '',
    })
    const formRef = ref<InstanceType<typeof ElForm>>()
    const rules = reactive({
      name: [
        {
          required: true,
          message: '请输入用户名',
          trigger: 'blur',
        },
        {
          min: 6,
          max: 8,
          message: '长度为6-8位数字',
          trigger: 'blur'
        },
     ],
      password: [
        {
          required: true,
          message: '请输入密码',
          trigger: 'blur',
        },
        {
          min: 6,
          max: 16,
          message: '长度为6-16位数字',
          trigger: 'blur'
        },
     ],
    })
    const loginCount = (isKeepLogin:boolean) => {
      formRef.value?.validate((value) => {
        if(value) {
          // 1. 记住密码做本地的缓存
          if (isKeepLogin) {
            Localcache.setCache("name", form.name)
            Localcache.setCache("password", form.password)
          } else {
            Localcache.deleteCache('name')
            Localcache.deleteCache('password')
          }
          console.log("form===", form)
          store.dispatch('LoginModule/actionLogin', {...form})
        }
      })
    }
    return {
      form,
      rules,
      loginCount,
      formRef
    }
  }
})
</script>
<style lang="less" scoped>
</style>